<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>添加删除记录练习</title>
    <script type="text/javascript">

        window.onload = function () {

            // 将删除tr的函数单独提取
            function delA(tr) {
                // var tr = this.parentNode.parentNode;
                var name = tr.getElementsByTagName('td')[0].innerHTML;
                if(confirm('确认删除【'+name+'】吗？')){
                    // 删除tr
                    tr.parentNode.removeChild(tr);
                }
                return false;
            }
            var addEmpButton = document.getElementById('addEmpButton');
            addEmpButton.onclick = function () {
                // 获取用户填写 姓名 邮件 薪资
                var name = document.getElementById('empName').value;
                var email = document.getElementById('email').value;
                var salary = document.getElementById('salary').value;

                // 创建一个tr
                var tr = document.createElement('tr');

                // 通过innerHTML向tr中添加内容
                tr.innerHTML = '<td>'+name+'</td>' +
                    '<td>'+email+'</td>' +
                    '<td>'+salary+'</td>' +
                    '<td><a href="javascript:;">Delete</a></td>';

                // 获取id为employeeTable的table
                var tbody = document.querySelector('#employeeTable tbody');
                tbody.appendChild(tr);



            };
            var tbody = document.querySelector('#employeeTable tbody');
            //利用事件委派来进行给超链接增加单击响应函数
            tbody.onclick = function (event) {

                if (event.target.nodeName.toLowerCase() === 'a'){
                    // delA();
                    delA(event.target.parentNode.parentNode);
                }
            };

        };

    </script>
</head>
<body>

<table id="employeeTable">
    <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Salary</th>
        <th>&nbsp;</th>
    </tr>
    <tr>
        <td>Tom</td>
        <td>tom@tom.com</td>
        <td>5000</td>
        <td><a href="javascript:;">Delete</a></td>
    </tr>
    <tr>
        <td>Jerry</td>
        <td>jerry@sohu.com</td>
        <td>8000</td>
        <td><a href="javascript:;">Delete</a></td>
    </tr>
    <tr>
        <td>Bob</td>
        <td>bob@tom.com</td>
        <td>10000</td>
        <td><a href="deleteEmp?id=003">Delete</a></td>
    </tr>
</table>

<div id="formDiv">

    <h4>添加新员工</h4>

    <table>
        <tr>
            <td class="word">name: </td>
            <td class="inp">
                <input type="text" name="empName" id="empName" />
            </td>
        </tr>
        <tr>
            <td class="word">email: </td>
            <td class="inp">
                <input type="text" name="email" id="email" />
            </td>
        </tr>
        <tr>
            <td class="word">salary: </td>
            <td class="inp">
                <input type="text" name="salary" id="salary" />
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <button id="addEmpButton">
                    Submit
                </button>
            </td>
        </tr>
    </table>

</div>

</body>
</html>
